﻿class com.crystalcentral.util.StringTool
{
    static var className : String = "StringTool";
    static var classVer : String = "1.0";
    static var authorName : String = "Kingda Sun";
    static var updateURL : String = "http://www.kingda.org/";
    
    
    //ltrim 消除左边空格
    static function ltrim (str : String) : String
    {
        var size = str.length;
        for (var i = 0; i < size; i ++)
        {
            if (str.charCodeAt (i) > 32) //解释：空格，tab,回车，换行charCode小于32
            {
                return str.substring (i);
            }
        }
        return "";
    }
    //rtrim 消除右边空格
    static function rtrim (str : String) : String
    {
        var size = str.length;
        for (var i = size; i > 0; i --)
        {
            if (str.charCodeAt (i) > 32) //同上
            
            {
                return str.substring (0, i + 1);
            }
        }
        return "";
    }
    //trim 消除两边空格
    static function trim (str : String) : String
    {
        return rtrim (ltrim (str));
    }
    //replace 使用字符串变量replaceWith，替换原字符串str中的子字符串replace
    static function replace (str : String, replace : String, replaceWith : String) : String
    {
        var sb : String = new String ();
        var found : Boolean = false;
        for (var i = 0; i < str.length; i ++)
        {
            if (str.charAt (i) == replace.charAt (0))
            {
                found = true;
                for (var j = 0; j < replace.length; j ++)
                {
                    if ( ! (str.charAt (i + j) == replace.charAt (j)))
                    {
                        found = false;
                        break;
                    }
                }
                if (found)
                {
                    sb += replaceWith;
                    i = i + (replace.length - 1);
                    continue;
                }
            }
            sb += str.charAt (i);
        }
        return sb;
    }
    //remove 删除原字符串中与字符串变量remove相同的子串
    static function remove(str:String, remove:String):String
    {
        return replace(str, remove, "");
    }
    
    //判断是否原字符串以子字符串s开头
    static function beginsWith(str:String, s:String):Boolean {
        return (s == str.substring(0, s.length));
    };

    //判断是否原字符串以子字符串s结尾
    static function endsWith(str:String, s:String):Boolean {
        return (s == str.substring(str.length - s.length));
    };
    
    //仅删除字符串中所有空格
    static function trimOnlySpace(str:String):String {
        var tmpAry:Array = str.split(" ");
        return tmpAry.join("");        
    }
    
    //删除字符串中所有空占位符，包括空格，回车，换行，制表，后退
    static function trimAllSpaces (str:String) : String
    {
        var tmpStr:String = trimOnlySpace(str);
        tmpStr = remove (tmpStr, "\n");
        tmpStr = remove (tmpStr, "\t");
        tmpStr = remove (tmpStr, "\r");
        tmpStr = remove (tmpStr, "\b");
        
        return tmpStr;
    }
    
    //删除字符串中所有标点符号，除了单词中可能必需的单引号
    static function trimPunc( str:String ):String {
    
        var marks:Array     = ["!",",",".",";",":","\"","(",")","?"];
        var nS:String = str;
        
        var iLength:Number = marks.length;
        for (var i:Number = 0; i< iLength; i ++ ) {
            nS = replace (nS, marks[i], "");
        }
        return nS;
    }
}